
package com.kanjian.music.fragment;

import java.util.ArrayList;

import android.os.Bundle;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.Loader;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.kanjian.music.R;
import com.kanjian.music.activity.BaseActivity;
import com.kanjian.music.adapter.RecommendListAdapter;
import com.kanjian.music.api.ApiRequestManager;
import com.kanjian.music.constants.IntentConstants;
import com.kanjian.music.entity.Music;
import com.kanjian.music.entity.Musician;
import com.kanjian.music.entity.Recommend;
import com.kanjian.music.network.HttpLoader;

/**
 * 推荐
 * 
 * @author xuyanjun@kanjian.com
 */
public class RecommendFragment extends MusicSubBaseFragment implements LoaderCallbacks<String> {

    private View mRootView;

    private RecommendListAdapter mListAdapter;

    ArrayList<Recommend> mRecommendList = new ArrayList<Recommend>();

    public static RecommendFragment newInstance() {
        RecommendFragment fragment = new RecommendFragment();
        Bundle b = new Bundle();
        fragment.setArguments(b);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mListAdapter = new RecommendListAdapter((BaseActivity) getActivity());
        initOldRecommendList();
    }

    private void initOldRecommendList() {

        Recommend recommend = new Recommend();
        recommend.mTitle = "我的订阅";
        recommend.mType = Recommend.RECOMMEND_TYPE_RECOMMEND_TITLE;
        mRecommendList.add(recommend);

        recommend = new Recommend();
        recommend.mType = Recommend.RECOMMEND_TYPE_SUBSCRIPTION;
        Music music = new Music();
        music.setMusicId(-1l);
        ArrayList<Music> subsMusiclist = new ArrayList<Music>();
        subsMusiclist.add(music);
        recommend.mSubscriptionMusic = subsMusiclist;
        mRecommendList.add(recommend);

        recommend = new Recommend();
        recommend.mType = Recommend.RECOMMEND_TYPE_SUBSCRIPTION;
        Musician musician = new Musician();
        musician.mUserId = -1;
        ArrayList<Musician> subsMusicianlist = new ArrayList<Musician>();
        subsMusicianlist.add(musician);
        recommend.mSubscriptionMusician = subsMusicianlist;
        mRecommendList.add(recommend);

        recommend = new Recommend();
        recommend.mTitle = "每日编辑推荐";
        recommend.mType = Recommend.RECOMMEND_TYPE_RECOMMEND_TITLE;
        mRecommendList.add(recommend);

        recommend = new Recommend();
        ArrayList<Musician> musicianList = new ArrayList<Musician>(4);
        Musician m = new Musician();
        m.mUserName = "musician1";
        musicianList.add(m);
        m = new Musician();
        m.mUserName = "musician2";
        musicianList.add(m);
        m = new Musician();
        m.mUserName = "musician3";
        musicianList.add(m);
        m = new Musician();
        m.mUserName = "musician4";
        musicianList.add(m);
        m = new Musician();
        m.mUserName = "musician5";
        musicianList.add(m);
        m = new Musician();
        m.mUserName = "musician6";
        musicianList.add(m);
        recommend.mType = Recommend.RECOMMEND_TYPE_RECOMMEND_MUSICIAN;
        recommend.mMusician = musicianList;

        mRecommendList.add(recommend);

        recommend = new Recommend();
        recommend.mTitle = "每日精选单曲";
        recommend.mType = Recommend.RECOMMEND_TYPE_RECOMMEND_TITLE;
        mRecommendList.add(recommend);

        Music tm = new Music();
        tm.setMusicId(-1l);
        for (int i = 0; i < 10; i++) {
            recommend = new Recommend();
            recommend.mType = Recommend.RECOMMEND_TYPE_RECOMMEND_MUSIC;
            recommend.mMusic = tm;
            mRecommendList.add(recommend);
        }
        mListAdapter.setDataList(mRecommendList);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (mRootView == null) {
            initView(inflater, container);
        }
        super.onCreateView(inflater, container, savedInstanceState);
        return mRootView;
    }

    private static final int LOADERID_SUBSCRIPTION = 0;

    private static final int LOADERID_RECOMMENDMUSICIAN = 1;

    private static final int LOADERID_NICEMUSIC = 2;

    private void initView(LayoutInflater inflater, ViewGroup container) {
        mRootView = inflater.inflate(R.layout.fragment_recommend, container, false);
        mListView = (ListView) mRootView.findViewById(R.id.recommend_listview);
        mListView.setAdapter(mListAdapter);
        mListAdapter.registerOnClickListener(mListView);
        Bundle args = new Bundle();
        args.putSerializable(IntentConstants.HTTPLOADERREQUESTPARAM,
                ApiRequestManager.getNiceRequest());
        getLoaderManager().restartLoader(LOADERID_NICEMUSIC, args, this);
        args = new Bundle();
        args.putSerializable(IntentConstants.HTTPLOADERREQUESTPARAM,
                ApiRequestManager.getRecommendRequest(false));
        getLoaderManager().restartLoader(LOADERID_RECOMMENDMUSICIAN, args, this);
        args = new Bundle();
        args.putSerializable(IntentConstants.HTTPLOADERREQUESTPARAM,
                ApiRequestManager.getMayLikeRequest(false));
        getLoaderManager().restartLoader(LOADERID_SUBSCRIPTION, args, this);
        setListScrollListener();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mRootView = null;
    }

    @Override
    public Loader<String> onCreateLoader(int id, Bundle args) {
        return new HttpLoader(getActivity(), args);
    }

    @Override
    public void onLoadFinished(Loader<String> loader, String data) {
        if (loader.getId() == LOADERID_SUBSCRIPTION) {
            ArrayList<Music> musicList = Music.getMusicListFromJson(data);
            ArrayList<Musician> musicianList = Musician.getMusicianListFromJson(data);
            if (musicList != null && musicList.size() > 0 && musicianList != null
                    && musicianList.size() > 0) {
                ArrayList<Recommend> tRecommendList = new ArrayList<Recommend>(2);
                Recommend tRecommend = new Recommend();
                tRecommend.mType = Recommend.RECOMMEND_TYPE_SUBSCRIPTION;
                tRecommend.mSubscriptionMusic = musicList;
                tRecommendList.add(tRecommend);
                tRecommend = new Recommend();
                tRecommend.mType = Recommend.RECOMMEND_TYPE_SUBSCRIPTION;
                tRecommend.mSubscriptionMusician = musicianList;
                tRecommendList.add(tRecommend);
                replaceRecommendList(tRecommendList);
            } else {
                ArrayList<Recommend> tRecommendList = new ArrayList<Recommend>(2);
                Recommend tRecommend = new Recommend();
                tRecommend.mType = Recommend.RECOMMEND_TYPE_SUBSCRIPTION;
                Music music = new Music();
                music.setMusicId(-2l);
                ArrayList<Music> subsMusiclist = new ArrayList<Music>();
                subsMusiclist.add(music);
                tRecommend.mSubscriptionMusic = subsMusiclist;
                tRecommendList.add(tRecommend);
                tRecommend = new Recommend();
                tRecommend.mType = Recommend.RECOMMEND_TYPE_SUBSCRIPTION;
                Musician musician = new Musician();
                musician.mUserId = -2;
                ArrayList<Musician> subsMusicianlist = new ArrayList<Musician>();
                subsMusicianlist.add(musician);
                tRecommend.mSubscriptionMusician = subsMusicianlist;
                tRecommendList.add(tRecommend);
                replaceRecommendList(tRecommendList);
            }
        } else if (loader.getId() == LOADERID_RECOMMENDMUSICIAN) {
            ArrayList<Musician> musicianList = Musician.getMusicianListFromJson(data);
            if (musicianList != null) {
                if (musicianList.size() > 6) {
                    for (int i = 0; i < musicianList.size() - 6; i++) {
                        musicianList.remove(i + 5);
                    }
                }
                ArrayList<Recommend> tRecommendList = new ArrayList<Recommend>();
                Recommend tRecommend = new Recommend();
                tRecommend.mType = Recommend.RECOMMEND_TYPE_RECOMMEND_MUSICIAN;
                tRecommend.mMusician = musicianList;
                tRecommendList.add(tRecommend);
                replaceRecommendList(tRecommendList);
            }
        } else if (loader.getId() == LOADERID_NICEMUSIC) {
            ArrayList<Music> musicList = Music.getMusicListFromJson(data);
            if (musicList != null) {
                int size = musicList.size();
                ArrayList<Recommend> tRecommendList = new ArrayList<Recommend>(size);
                Recommend tRecommend;
                for (int i = 0; i < size; i++) {
                    tRecommend = new Recommend();
                    tRecommend.mType = Recommend.RECOMMEND_TYPE_RECOMMEND_MUSIC;
                    tRecommend.mMusic = musicList.get(i);
                    tRecommendList.add(tRecommend);
                }
                replaceRecommendList(tRecommendList);
            }
        }

        if (data != null) {
            mListAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onLoaderReset(Loader<String> loader) {
    }

    private synchronized void replaceRecommendList(ArrayList<Recommend> recommendList) {
        if (recommendList != null && recommendList.size() > 0) {
            int type = recommendList.get(0).mType;
            if (type == Recommend.RECOMMEND_TYPE_SUBSCRIPTION) {
                mRecommendList.remove(1);
                mRecommendList.remove(1);
                mRecommendList.addAll(1, recommendList);
            } else if (type == Recommend.RECOMMEND_TYPE_RECOMMEND_MUSICIAN) {
                mRecommendList.remove(4);
                mRecommendList.addAll(4, recommendList);
            } else if (type == Recommend.RECOMMEND_TYPE_RECOMMEND_MUSIC) {
                int size = mRecommendList.size();
                for (int i = 0; i < size - 6; i++) {
                    mRecommendList.remove(6);
                }
                mRecommendList.addAll(6, recommendList);
            }
            mListAdapter.notifyDataSetChanged();
        }
    }

}
